package com.liaoban.app.ui.base;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.liaoban.app.R;
import com.liaoban.app.ui.common.EmptyView;
import com.liaoban.app.util.CommonUtils;

import java.util.List;

import butterknife.BindView;


/**
 * activity 列表基类
 * Created by ChenYuYun on 2018/3/27.
 * 继承该类：实现其抽象方法即可
 */
public abstract class BaseListActivity<T> extends BaseUploadPicActivity {
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.toolbar)
    protected Toolbar toolbar;
    @BindView(R.id.rv_list)
    public RecyclerView rvList;
    @BindView(R.id.swipe_layout)
    protected SwipeRefreshLayout swipeLayout;
    public BaseQuickAdapter mAdapter;
    protected int mPageIndex = 1;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_list;
    }

    /**
     * 设备标题名称
     *
     * @return
     */
    public abstract String getNewTitle();

    public int getPageIndex() {
        return mPageIndex;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        swipeLayout.setOnRefreshListener(this::refresh);
        initRecyclerView(rvList);
        refresh();
    }

    protected void setRecyclerView(RecyclerView recyclerView) {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rvList.setLayoutManager(manager);
    }

    /**
     * 初始化RecyclerView控件,如果需要特殊处理样式时，可以重写这个方法
     *
     * @param recyclerView
     */
    private void initRecyclerView(RecyclerView recyclerView) {
        setRecyclerView(recyclerView);
        mAdapter = getAdapter();
        rvList.setAdapter(mAdapter);
        if(isPage()) {
            mAdapter.setOnLoadMoreListener(this::loadData, rvList);
            mAdapter.disableLoadMoreIfNotFullPage();
        }
        mAdapter.setEmptyView(new EmptyView(this));
        mAdapter.setOnItemClickListener(this::itemClick);
        mAdapter.setOnItemChildClickListener(this::itemChildClick);
    }

    /**
     * 是否分页
     * @return
     */
    protected boolean isPage() {
        return true;
    }

    /**
     * 继续加载数据
     */
    protected void loadData() {
        swipeLayout.setEnabled(false);
        getData();
    }

    /**
     * 刷新列表
     */
    protected void refresh() {
        mPageIndex = 1;
        loadData();
    }

    public abstract BaseQuickAdapter getAdapter();

    public abstract void itemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position);

    public abstract void itemClick(BaseQuickAdapter baseQuickAdapter, View view, int position);

    public abstract void getData();


    //数据请求成功后的结果处理
    public void doSuc(List<T> rows, int totalPageCount) {
        swipeLayout.setRefreshing(false);
        swipeLayout.setEnabled(true);
        mAdapter.loadMoreComplete();
        //if (!CommonUtils.isEmpty(rows)) {
            if (mPageIndex == 1) {
                mAdapter.setNewData(rows);
            } else {
                mAdapter.addData(rows);
            }
        //}
        if (mPageIndex++ >= totalPageCount) {
            mAdapter.loadMoreEnd(false);
        }
    }

    //数据请求后失败处理
    protected void doError(Throwable throwable) {
        swipeLayout.setEnabled(true);
        swipeLayout.setRefreshing(false);
        mAdapter.loadMoreFail();
        defOnError(throwable);
    }

}
